class OutController < ApplicationController

  before_filter :verify_session,:gettag_counts
  
  def index
  end
  
  def list
    @outrecords = OutRecord.find_by_sql('select out_records.* from out_records,books '+
    'where out_records.book_id=books.id and books.book_status_id=2')
    @outrecords_pages =Paginator.new self, @outrecords.length , 10, params[:page]
  end
  
  def new
    init_new(true)
    if @books.size==0
      flash[:notice]="当前所有图书都已经借出,请找图书管理员协商."
      redirect_to :action => 'list'
    end
    @out_record = OutRecord.new
#    默认的还书日期是借书日期之后的一个月
    @out_record.date = Date.today
    @out_record.return_date = @out_record.date >> 1
  end
  
  def create
    @out_record = OutRecord.new(params[:out_record])
    begin
      OutRecord.transaction do
        book = @out_record.book
        book.book_status_id = 2
        book.update
        @out_record.save!()
        deliver_email(@out_record)
        flash[:notice] = "借书操作成功!"
        redirect_to :action => 'list'
      end
    rescue
      flash[:notice] = "借书操作失败!"
      init_new
#      redirect_to :action => 'new'
      render :action => :new
    end
  end
  
  def edit
    init_new(false)
    @out_record = OutRecord.find(params[:id])
  end
  
  def update
    @out_record = OutRecord.find(params[:id])
    if @out_record.update_attributes(params[:out_record])
      flash[:notice] = "借书信息更新成功!"
    elsif
      flash[:notice] = "借书信息更新失败!"
    end
    list
    render :action => 'list'
  end
  
  def search
    @readers = Reader.find(:all)
    @books = Book.find(:all)
    @books_pages, @books = paginate :books, :per_page => 10  
  end
  
  def execute_search_reader
    reader_id = params[:readers][:id]
    search_userOutrecord(reader_id)
  end
  
  def search_myOut
    reader_id = session[:user].id
    @mybook=true
    search_userOutrecord(reader_id)
  end
  
  def search_userOutrecord(reader_id)
     @outrecords = OutRecord.find_by_sql('select out_records.* from out_records,books '+
    'where out_records.book_id=books.id and out_records.reader_id = '+reader_id.to_s+' and (books.book_status_id=2 or books.book_status_id = 5)')
    
    if @outrecords.size == 0
      flash[:notice]="没有该用户的借书记录！"
    end
    render :action => 'search_result'
  end
  
  def execute_search_book
    book_id = params[:books][:id]
    @outrecords = OutRecord.find(:all,:conditions => ['book_id = ?',book_id])
    
    if @outrecords.size == 0
      flash[:notice]="没有这本书的借书记录！"
    end
    
    render :action => 'search_result'
  end
  
#  用户主动请求借书
  def self_borrow
    out_record = OutRecord.new
    out_record.date = Date.today
    out_record.return_date = out_record.date >> 1
    out_record.reader = session[:user]
    
    @book = Book.find(params[:id])
    @book.book_status_id = 4
    @book.update
    
    out_record.book = @book
    out_record.save!()
    
    deliver_newOutNotify(out_record)
    render(:text => '您的操作请求已经成功发给管理员,请到管理员那里核实信息,并领取图书.')
  end
  
  
  def showNeedcheckRecords
  end
  
#  显示所有的待批图书
  def showNeedcheckOutRecords
    @outrecords = OutRecord.find_by_sql('select out_records.* from out_records,books where out_records.book_id=books.id and (books.book_status_id=4)')
    render :layout => false
  end
  
#  管理员确认借出图书
  def outSubmit
    book = Book.find(params[:id])
    book.book_status_id = 2
    book.update
    deliver_email(book.out_record)
    redirect_to :action => 'showNeedcheckRecords'
  end
  
  private
  #为借书页面初始化需要的参数,图书列表和人员列表
  def init_new(is_new)
    @readers = Reader.find(:all)
    if(is_new)
    #    取出状态为'闲置'的图书
      @books = Book.find(:all , :conditions => "book_status_id = 1")
    elsif
      @books = Book.find(:all)
    end
  end
 
 
  def deliver_email(out_record)
     email = BookMailer.create_borrowbook(out_record.reader.email,out_record)
     BookMailer.deliver(email)
  end
  
  def deliver_newOutNotify(out_record)
    email =  BookMailer.create_newOutRecord(out_record)
    BookMailer.deliver(email)
  end
  
end
